﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CATLTempUpdate.CommonLib
{
    public class Log
    {
        private static string logPath = string.Empty;

        public static List<string> Trace = new List<string>();
        public static List<string> SystemError = new List<string>();
        public static List<string> PLCTrace = new List<string>();
        public static List<string> SQL = new List<string>();
        public static List<string> MeasureError = new List<string>();

        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        /// 
        public static string LogPath
        {
            get
            {
                logPath = AppDomain.CurrentDomain.BaseDirectory + "/log/";

                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return logFielPrefix; }
            set { logFielPrefix = value; }
        }

        public Log()
        {
            logFielPrefix = "";
        }


        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, List<string> msg, int DataLength)
        {
            try
            {
                string ipath = LogPath + LogFielPrefix + logFile + "/ " + DateTime.Now.Year + "/" + DateTime.Now.Month;
                if (!Directory.Exists(ipath))
                {
                    Directory.CreateDirectory(ipath);
                }
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    ipath + "/" +
                    logFile + "_" + DateTime.Now.ToString("yyyyMMdd") + ".Log"
                    );
                for (int i = 0; i < DataLength; i++)
                {
                    sw.WriteLine(msg[i]);
                    sw.WriteLine();
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                Log.SystemError.Add(DateTime.Now + " " + ex.Message);
            }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogFile logFile, List<string> msg, int DataLength)
        {
            WriteLog(logFile.ToString(), msg, DataLength);
        }
        /// <summary>
        /// 写日志
        /// </summary>
        public static void AddLog(LogFile logFile, string msg)
        {
            if (logFile == LogFile.Trace)
            {
                Log.Trace.Add(DateTime.Now + " " + msg);
            }
            if (logFile == LogFile.SystemError)
            {
                Log.SystemError.Add(DateTime.Now + " " + msg);
            }
            if (logFile == LogFile.PLCTrace)
            {
                Log.PLCTrace.Add(DateTime.Now + " " + msg);
            }
            if (logFile == LogFile.SQL)
            {
                Log.SQL.Add(DateTime.Now + " " + msg);
            }
            if (logFile == LogFile.MeasureError)
            {
                Log.MeasureError.Add(DateTime.Now + " " + msg);
            }
        }
    }


    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Trace,
        SystemError,
        PLCTrace,
        SQL,
        MeasureError
        
    }
}